import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter_application_1/models/home_banner_data.dart';
import 'package:flutter_application_1/models/home_article_data.dart';
import '../../apis/api.dart';

class HomeViewModel with ChangeNotifier {
  List bannerList = [];
  List articleList = [];
  num listCount = 0;

  // 获取banner
  Future getBanner() async {
    try {
      List list = await Api.instance.getBanner();
      bannerList = list;
    } catch (e) {
      bannerList = [];
    } finally {
      notifyListeners();
    }
  }

  // 初始化文章list
  Future initArticleList(bool more) async {
    if (more) {
      listCount++;
      getArticleList(listCount);
      debugPrint('listCount${listCount.toDouble()}');
    } else {
      listCount = 1;
      articleList.clear();
      await getArticleTopList();
      await getArticleList(listCount);
    }
  }

  // 获取文章list
  Future getArticleList(count) async {
    try {
      List list = await Api.instance.getArticleList(count);
      articleList.addAll(list);
      debugPrint("articleList1:${articleList.length}");
    } catch (e) {
      articleList = [];
      debugPrint("articleListError:${e.toString()}");
    } finally {
      notifyListeners();
    }
  }

  // 获取文章toplist
  Future getArticleTopList() async {
    try {
      articleList.clear();
      List list = await Api.instance.getArticleTopList();
      articleList.addAll(list);
      debugPrint("articleList:${articleList.length}");
    } catch (e) {
      debugPrint("articleListTop:${articleList.length}, ${e.toString()}");
      articleList = [];
    }
  }
}
